package com.crm.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.dto.CourseDto;
import com.crm.dto.CoursePageDto;
import com.crm.entity.Course;
import com.crm.result.PageResultTow;
import com.crm.result.Result;
import com.crm.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/clues/course")
public class CourseController {

   @Autowired
    private CourseService courseService;

    // 添加课程
    @PostMapping
    public Result<Boolean> add(@RequestBody CourseDto courseDto) {
        log.info("接收到添加课程：{}", courseDto.getName());
        courseService.add(courseDto);
        return Result.success();
    }

    // 修改课程
    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        log.info("接收到修改课程：{}", course.getName());
        courseService.updateCourse(course);
        return Result.success();
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Course course = courseService.getById(id);
        return Result.success(course);
    }
    //根据subject查询课程，然后返回对应的课程信息
    @GetMapping("/listselect")
    public Result<List<Course>> getCoursesBySubject(@RequestParam String subject) {
        List<Course> courses = courseService.getCoursesBySubject(subject);
        return Result.success(courses);
    }

    @GetMapping(path = "/listselect", params = "!subject")
    public Result<List<Course>> selectAll() {
        List<Course> courses = courseService.selectAll();
        return Result.success(courses);
    }



    @DeleteMapping("/{ids}")
    public ResponseEntity<Boolean> batchDeleteCourses(@PathVariable Long[] ids) {
        boolean success = courseService.batchDeleteCourses(ids);
        log.info("批量删除课程完成，共删除 {} 条记录", ids);
        return ResponseEntity.ok(true);
    }


    // 条件分页查询
    @GetMapping("/list")
    public PageResultTow search(CoursePageDto coursePageDto)
    {
        Page<Course> coursesByCondition = courseService.getCoursesByCondition(coursePageDto);

         return PageResultTow.success(coursesByCondition.getRecords(), coursesByCondition.getTotal() ) ;
    }

}
